import Vue from 'vue'
import store from '../store'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/home',
    component: () => import('@/views/Home.vue'),
	children:[
		{
			path: '/index',
			component: () => import('@/components/index/Index.vue')
		},
		{
			path:"/",
			redirect:"/index"
		},
		{
			path: '/goods',
			component: () => import('@/components/goods/Goods.vue')
		},
		{
			path: '/detail',
			component: () => import('@/components/details/Detail.vue')
		},
		{
			path: '/about',
			component: () => import('@/components/about/About.vue')
		},
		{
			path: '/collect',
			component: () => import('@/components/collect/Collect.vue')
		},
		{
			path: '/shoppingCart',
			component: () => import('@/components/shoppingCart/ShoppingCart.vue')
		},
		{
			path: '/confirmOrder',
			component: () => import('@/components/confirm/Confirm.vue')
		},
	]
  },
  {
	  path:"/",
	  redirect:"/home"
  }
]

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location, onResolve, onReject) {
    if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
    return originalPush.call(this, location).catch(err => err)
};

const router = new VueRouter({
  routes
})

router.beforeEach((to,from,next)=>{
    if(to.path =='/order' || to.path =='/collect' || to.path =='/shoppingCart'){
        if(store.state.user){
			next()
		}else{
			next(from.path+"?loginFlag=true")
		}
    }else{
        next(true)
    }
})


export default router
